﻿///<reference path="../../../../Scripts/jquery-1.4.1-vsdoc.js"/>

function BibleBookPane(id, parentControl) {
    var thisId = id;

    this.ParentControl = parentControl;
    var container;
    this.BookAllView = true;
    var currentBook;
    var currentChapter;

    this.Render = function () {
        container = $("<div>", {
            id: thisId,
            className: "bibleBookPane windowFullHeight"
        }).appendTo(this.ParentControl);
    }

    this.GetContainer = function () {
        return container;
    }

    this.GetCurrentBook = function () {
        return currentBook;
    }

    this.GetCurrentChapter = function () {
        return currentChapter;
    }

    this.GetCurrentBookKey = function () {
        if (currentBook != undefined && currentBook != null)
            return currentBook.Key;

        return undefined;
    }

    this.GetCurrentChapterKey = function () {
        if (currentChapter != undefined && currentChapter != null)
            return currentChapter.Key;

        return undefined;
    }    

    this.ShowBook = function (bookKey) {
        if (this.GetCurrentBookKey() != bookKey || this.GetCurrentChapterKey() != undefined) {
            var book = appPointer.BiblePlayer.GetBook(bookKey);

            if (this.BookAllView) {
                var xml = appPointer.CacheManager.GetCacheItem(bookKey);
                container.html(TransformNode(xml, "xslt/book.xslt"));
                currentBook = book;
                currentChapter = undefined;
            }
            else
                this.ShowBookChapter(book.GetChapterByIndex(0).GetKey());
        }

        if (this.BookAllView)  // иначе в методе ShowBookChapter() будет scrollTop
            container.scrollTop(0);
    }

    this.ShowBookChapter = function (chapterKey, force) {
        if (force == undefined)
            force = false;

        if (this.BookAllView) {
            var bookKey = appPointer.KeyManager.GetBookKeyFromChapterKey(chapterKey);
            if (this.GetCurrentBookKey() != bookKey || force) {
                this.ShowBook(bookKey);
            }

            container.find("div.chapter")[appPointer.KeyManager.GetChapterIndexFromKey(chapterKey)].scrollIntoView();

            currentChapter = undefined;
        }
        else {  // показываем только главу            
            if (this.GetCurrentChapterKey() != chapterKey || force) {
                var bookKey = appPointer.KeyManager.GetBookKeyFromChapterKey(chapterKey);
                var book = appPointer.BiblePlayer.GetBook(bookKey);
                var chapter = book.GetChapter(chapterKey);
                var xml = chapter.Xml;

                currentChapter = chapter;

                container.html(TransformNode(xml, "xslt/chapter.xslt"));
                
            }

            container.scrollTop(0);
        }
    }

    this.HighlightVerse = function (verseKey, searchPattern) {
        var chapterKey = FindDisplayChapterKey();
        if (chapterKey != undefined) {
            var chapterIndex = appPointer.KeyManager.GetChapterIndexFromKey(appPointer.KeyManager.GetChapterKeyFromVerseKey(verseKey));
            var verseIndex = appPointer.KeyManager.GetVerseIndexFromKey(verseKey);
            var verse = $(container.find("div.chapter")[chapterIndex]).find("div.verseText")[verseIndex];
            var $verse = $(verse);
            container.removeHighlight();
            $verse.highlight(searchPattern);
            verse.scrollIntoView(); // ??? как то не очень красиво..., но оч редко полезно. надо бы проверять - если не видно, тогда уже делать такое...
        }        
    }

    // определяет наиболее показываемую главу. если книга не загружена - возвращает undefined
    function FindDisplayChapterKey() {
        if (currentChapter != undefined && currentChapter != null)
            return currentChapter.Key;           

        var minOffsetTop = 1000;
        var minOffsetTopChapter = undefined;

        $("div.chapter", container).each(function () {
            var chapter = $(this);
            var chapterOffsetTop = chapter.offset().top;
            if (chapterOffsetTop < 0)
                chapterOffsetTop -= 300;
            chapterOffsetTop = Math.abs(chapterOffsetTop);
            if (chapterOffsetTop <= minOffsetTop) {
                minOffsetTop = chapterOffsetTop;
                minOffsetTopChapter = this;
            }
        });
        
        if (minOffsetTopChapter != undefined)
            return minOffsetTopChapter.id;

        return undefined;
    }

    this.OnBookViewChanged = function () {
        this.BookAllView = !this.BookAllView;

        var displayChapterKey = FindDisplayChapterKey();
        if (displayChapterKey != undefined) {
            this.ShowBookChapter(displayChapterKey, true);
        }
    }
}